package com.simple.simple_view.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.simple.simple_view.domain.entity.LoginUser;
import com.simple.simple_view.domain.entity.User;
import com.simple.simple_view.domain.enums.AppHttpCodeEnum;
import com.simple.simple_view.exception.SystemException;
import com.simple.simple_view.mapper.UserMapper;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/*
 *@description: TODO
 *@author: hisen
 *@Time: 2023/10/29  09:21
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,username);
        User user = userMapper.selectOne(queryWrapper);

        if(Objects.isNull(user))throw new SystemException(AppHttpCodeEnum.LOGIN_ERROR);

        return new LoginUser(user);
    }
}